/**
 * Created by gongxun on 17/1/13.
 */

var GsbdxCardHelper = function(){
    var that = {};

    that.design = cc.size(42, 60);

    that.inhandScale = 1;

    that.inhandMyScale = 1;

    that.cardWellScale = 1;

    that.seats = null;

    that.init = function(){
        that.inhandScale = 1;
        that.inhandMyScale = 1.5;
        that.cardWellScale = 0.8;

        that.seats = [];
        that.seats.push(cc.p(mo.gameSize.x / 2, 180));
        that.seats.push(cc.p(mo.gameSize.x - 170, mo.gameSize.y - 300));
        that.seats.push(cc.p(mo.gameSize.x - 275, mo.gameSize.y - 155));
        that.seats.push(cc.p(275, mo.gameSize.y - 155));
        that.seats.push(cc.p(170, mo.gameSize.y - 300));
    };

    that.getCardSize = function(){
        var size = cc.size(that.design.width * that.inhandScale, that.design.height * that.inhandScale);
        return size;
    };

    that.getMyCardSize = function(){
        var size = cc.size(that.design.width * that.inhandMyScale, that.design.height * that.inhandMyScale);
        return size;
    };

    that.getExistCardNum = function (cardNum) {
        if ((cardNum >= 41 && cardNum <= 49) || cardNum == 87){
            return true;
        }
        return false;
    };

    that.getRandomCard = function(size){
        var result = [];
        for (var i = 0; i < size; i++){
            var temp = parseInt(Math.random() * 10) + 41;
            if (temp == 50){
                temp = 87;
            }
            result.push(temp);
        }
        return result;
    };

    //返回手牌位置
    that.getInhandCardPos = function(seat, index){
        var cardSize = that.getCardSize();
        if (seat == 0){
            cardSize = that.getMyCardSize();
        }
        var pos = cc.p(that.seats[seat].x, that.seats[seat].y);
        pos.y -= (cardSize.height / 2);
        var offset = 3;
        pos.x = (index == 0 ? pos.x - cardSize.width / 2 - offset : pos.x + cardSize.width / 2 + offset);
        return pos;
    };

    //返回牌墙位置
    that.getHideCardPos = function(index, center){
        center = cc.p(center.x, center.y + 90);
        var cardSize = cc.size(that.design.width * that.cardWellScale, that.design.height * that.cardWellScale);
        //row 10
        //col 2
        var row = index % 10;
        var col = parseInt(index / 10);
        var x = center.x + (row - 4.5) * cardSize.width;
        var y = center.y + 14 * that.cardWellScale * col;
        return cc.p(x, y);
    };

    //返回牌墙显示位置
    that.getShowCardPos = function(index, center){
        center = cc.p(center.x, center.y + 90);
        var cardSize = cc.size(that.design.width * that.cardWellScale, that.design.height * that.cardWellScale);
        //row 10
        //col 2
        var row = index % 10;
        var col = parseInt(index / 10);
        var x = center.x + (row - 4.5) * cardSize.width;
        var offset = 20;
        var y = center.y + 14 * that.cardWellScale * col - that.design.height * that.cardWellScale - offset;
        return cc.p(x, y);
    };

    //返回表情序列帧
    that.getFaceFrames = function (index) {
        var spriteFrames = [];
        for (var i = 0; i <= 60; i++){
            var frame = cc.spriteFrameCache.getSpriteFrame("emoji_" + index + "_" + i + ".png");
            if (frame == null){
                break;
            }
            spriteFrames.push(frame);
        }
        return spriteFrames;
    };

    //检测值是否存在
    that.getIsNumExist = function(num1, num2, cardNums){
        if ((num1 + 40) == cardNums[0] && (num2 + 40) == cardNums[1]){
            return true;
        }

        if ((num1 + 40) == cardNums[1] && (num2 + 40) == cardNums[0]){
            return true;
        }

        return false;
    };

    //返回牌型的声音文件
    that.getCardTypeSoundFile = function(cardNums){
        var card1 = cardNums[0];
        var card2 = cardNums[1];

        var result = "";

        //豹子 大豹子
        if (card1 == card2){
            if (card1 == 87){
                result = "ttz_dabaozi.mp3";
            }else{
                result = "ttz_baozi.mp3";
            }
        }else{
            //特殊牌
            var specialNums = [
                [1, 8],
                [2, 5],
                [3, 5],
                [3, 6],
                [4, 5],
                [5, 8]
            ];
            var isExistSpecialType = false;
            for (var i = 0; i < specialNums.length; i++){
                if (that.getIsNumExist(specialNums[i][0], specialNums[i][1], cardNums)){
                    result = "ttz_s_" + specialNums[i][0] + "_" + specialNums[i][1] + ".mp3";
                    isExistSpecialType = true;
                    break;
                }
            }

            if (isExistSpecialType){

            }else{
                if (card1 == 87 || card2 == 87){
                    var temp = (card1 + card2 + 3) % 10 + 20;
                    result = "ttz_type_" + temp + ".mp3";
                }else if (card1 == 49 || card2 == 49){
                    var temp = (card1 + card2) % 10 + 10;
                    result = "ttz_type_" + temp + ".mp3";
                }else{
                    var temp = (card1 + card2) % 10;
                    result = "ttz_type_" + temp + ".mp3";
                }
            }

        }

        return result;
    };

    return that;
};

mo.gsbdxCardHelper = new GsbdxCardHelper();